package com.lineengine.lineengine.node;


import cn.hutool.core.date.SystemClock;
import com.lineengine.lineengine.context.IContext;
import com.lineengine.lineengine.result.IResult;

import java.util.function.Supplier;

/**
 * <p>
 * 节点
 * </p>
 *
 * @author wenrong.yu
 * @since 2020-11-07 21:20
 */
@FunctionalInterface
public interface INode<T extends IContext<T>> {
    /**
     * 执行
     *
     * @param context 上下文
     * @return 执行结果
     */
    IResult<T> execute(T context);

    default IResult<T> processWithRecordCostTime(T context, Supplier<IResult<T>> supplier) {
        long start = SystemClock.now();
        IResult<T> result = supplier.get();
        context.recordCostTime(this.getClass().getSimpleName(), SystemClock.now() - start);
        return result;
    }
}
